#!/usr/bin/env node
"use strict";
const fs = require('fs'),
      path = require("path"),
        FILE_TYPE = 'json',
      error = [
	      `ERROR : The file type must be 'JSON'`,
	      `ERROR : The file This file does not have a 'pages' item`
	    ],
      wxAppContent = {
        '.js' : `App({
  onLaunch: function() { 
    // Do something initial when launch.
  },
  onShow: function() {
      // Do something when show.
  },
  onHide: function() {
      // Do something when hide.
  },
  globalData: 'I am global data'
})`,
        '.wxss' : `/* public style */`, 
      },
	    wxPageContent = {
	  	  '.js' : `//get global object
const app = getApp();

Page({
  data: {
    text: "This is page data."
  },
  onLoad: function(options) {
    // Do some initialize when page load.
  },
  onReady: function() {
    // Do something when page ready.
  },
  onShow: function() {
    // Do something when page show.
  },
  onHide: function() {
    // Do something when page hide.
  },
  onUnload: function() {
    // Do something when page close.
  },
  onPullDownRefresh: function() {
    // Do something when pull down.
  },
  onReachBottom: function() {
    // Do something when page reach bottom.
  },
  // Event handler.
  viewTap: function() {
    this.setData({
      text: 'Set some data for updating view.'
    })
  },
  customData: {
    hi: 'MINA'
  }
})`,
	  	  '.json' : `{
  "navigationBarBackgroundColor": "#ffffff",
  "navigationBarTextStyle": "black",
  "navigationBarTitleText": "",
  "backgroundColor": "#eeeeee",
  "backgroundTextStyle": "light"
}`,
	  	  '.wxml' : `<view> {{text}} </view>`,
	  	  '.wxss' : `view {
  	padding:10px;
}`
  	  },
  	  fileName = process.argv[2].replace(/\\/g, "/"),
  	  file = {
  	      path : fileName,
  			  type : fileName.split(".").reverse()[0],
  			  name : fileName.split("/").reverse()[0].split('.')[0] ,
  			  root : fileName.replace(fileName.split("/").reverse()[0], "")
  	  },
  	  mkdirsSync = (dirname = '', mode = null) => { 
  		    if(fs.existsSync(dirname)){
  		        return true;
  		    }else{
  		        if(mkdirsSync(path.dirname(dirname), mode)){
  		            fs.mkdirSync(dirname, mode);
  		            return true;
  		        } else {
                  return false;
              }
  		    }
  	  }

if ( file.type !== FILE_TYPE ) throw(error[0]);
let appConfig = JSON.parse(fs.readFileSync(file.path, "utf-8"));
if ( !appConfig.pages ) throw(error[1]);

for (let key in wxAppContent) {
  let fileName = file.root + file.name + key;
  void(fs.writeFileSync(fileName, wxAppContent[key]));
  if ( fs.statSync(fileName) ) {
    console.log(`info : mkfile '${fileName}' success`); 
  } else {
    console.log(`info : mkfile '${fileName}' error`); 
  }
}

appConfig.pages.forEach((dirName) => {
	let newDirName = dirName.split("/");
	void(newDirName.pop());
	newDirName = file.root + newDirName.join("/");
	console.log(newDirName);
	console.log(`info : mkdir '${newDirName}' is ` + mkdirsSync(newDirName)); 
	for (let key in wxPageContent) { 
    let fileName = file.root + dirName + key;
		void(fs.writeFileSync(fileName, wxPageContent[key]));
    if ( fs.statSync(fileName) ) {
      console.log(`info : mkfile '${fileName}' success`); 
    } else {
      console.log(`info : mkfile '${fileName}' error`); 
    }
	}
});